<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=$this->plugin_setting('setting');
$mid=$_RQ['mid']?$_RQ['mid']:$_SESSION['uid'];
$myshop=pdo_get('jy_newshopfee_shop',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
if(!$myshop['mid']){
	exi('','error','jy_newshopfee/index/');
}
if($_RQ['op']=='main'){
	$return['mid']=$mid;
	$return['sharedata']=array(
			'title'=>$myshop['title']?$myshop['title']:'我的小店',
			'desc'=>$myshop['desc']?:$myshop['title'].'描述',
			'link'=>custom_url('jy_newshopfee/agent.myshop/main/mid='.$mid,true),
			'imgUrl'=>tomedia($myshop['logo']),
	);
	$return['member']=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('id','avatar','nickname'));
	$myshop['logo']=$myshop['logo']?$myshop['logo']:$return['member']['avatar'];
	$return['setting']=$setting;
	$level=pdo_getcolumn('jy_newshopfee_member',array('mid'=>$_SESSION['uid']),'level_id');
	if($level){
		$return['level']=pdo_getcolumn('jy_newshopfee_level',array('id'=>$level),'title');	
	}else{
		$return['level']='普通等级';
	}
	$return['ordercount']=$total['total'];
	$goodsids = explode(';',$myshop['goodsids']);
	if($myshop['selectgoods']){
		$goodsids = explode(';',$myshop['goodsids']);
		$params['id']=$goodsids;
		$return['goodscount']=count($goodsids)-2;
	}else{
		$return['goodscount']=pdo_count('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	}
	$myshop['title'] = $myshop['title']?$myshop['title']:$return['member']['nickname'].'的小店';
	$myshop['logo'] = $myshop['logo']?tomedia($myshop['logo']):$return['member']['avatar'];
	if($_SESSION['wxapp_id']){
		$image='oss/'.$_SESSION['uniacid'].'/images/jy_newshopfee/wxappid'.$_SESSION['uid'].'.jpg';
		$isadd=cfc('files')->source_check($image);
		if(!$isadd){
			$url='/jy_newshopfee/agent.myshop/index?mid='.$_SESSION['uid'].'&fromuser='.$_SESSION['uid'];
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			if($_SESSION['fromh5wxapp']){
				$url='/jy_newshopfee/agent.myshop//&mid='.$_SESSION['uid'].'&fromuser='.$_SESSION['uid'];
				$url='/pages/index/index?url='.urlencode($url);
			}
			$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
			$path='oss/'.$_SESSION['uniacid'].'/images/jy_newshopfee/';
			if(!file_exists(ROOT_AT.$path)){
				mkdir(ROOT_AT.$path,0700, true);
			}
			$filename=$path.'/wxappid'.$_SESSION['uid'].'.jpg';
			cfc('files')->source_save($res,'image','png','二维码',$filename,'',$url);
		}
		$myshop['qrcode']=tomedia($image);
	}
	$myshop['title1']=cfc('tools')->substr($myshop['title'],0,13);
	$myshop['title2']=cfc('tools')->substr($myshop['desc'],0,13);
	$myshop['title2']=$myshop['title2']?$myshop['title2'].'...':'暂无描述';
	$return['myshop']=$myshop;
	$return['params']=array('page'=>1);
	set_html('title',$myshop['title']);
	exi($return);
}
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($myshop['selectgoods']){
		$goodsids = explode(';',$myshop['goodsids']);
		$params['id']=$goodsids;
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
			$return['params']['id']=intval($_RQ['keyword']);
		}else{
			$params['title like']='%'.trim($_RQ['keyword']).'%';
			$return['params']['title']=$_RQ['keyword'];
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$parameter=array(
			'params'=>$params,
			'limit'=>array($pindex,$psize),
			'fields'=>$fields,
			'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}
	if($list){
		$install = check_permission('jy_merch');
		foreach($list as &$itemv){
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;
			
			if($install){
				$itemv['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$itemv['merchid']),'merchname');
			}
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$itemv['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'total');
			$itemv['cartid']=pdo_getcolumn('jy_weishop_member_cart',array('goodsid'=>$itemv['id'],'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
		}
	}
	exi($list);
}
?>